package huu.phong.youtube;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.util.ArrayDeque;
import java.util.Queue;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
import javax.swing.border.EmptyBorder;
import javax.swing.border.MatteBorder;


public class SongRenderer implements ListCellRenderer{
	public static final ImageIcon DEFAULT_DETAIL_ALBUM_ART = new ImageIcon(SongRenderer.class.getResource("/images/default_albumart.jpg"));
	public static final ImageIcon DEFAULT_CLASSIC_ALBUM_ART = new ImageIcon(SongRenderer.class.getResource("/images/song.png"));
	Color odd = new Color(238, 238, 238);
	Color even = Color.WHITE;
	Color selected = new Color(51, 153, 255);
	Color line = new Color(238, 238, 238);
	public static final int VIEW_MODE_CLASSIC = 1;
	public static final int VIEW_MODE_DETAIL = 0;
	Dimension dimension;
	Dimension imageSize = new Dimension(120, 90);
	Thread loadImages;
	Queue<Thread> process = new ArrayDeque<Thread>();
	MatteBorder matteBorder = new MatteBorder(0, 0, 1, 0, line);
	EmptyBorder emptyBorder = new EmptyBorder(0, 4, 0, 0);
	JPanel panel;
	JLabel icon, text;
	
	public SongRenderer(){
		loadImages = new Thread(){
			public void run(){
				while (true){
					while (!process.isEmpty()){
						Thread thread = process.poll();
						thread.start();
						try {
							thread.join();
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
					synchronized (loadImages) {
						try {
							loadImages.wait();
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
			}
		};
		loadImages.start();
	}
	
	public Component getListCellRendererComponent(final JList list, Object value, int index, boolean isSelected, boolean cellHasFocus){
		final Video song = (Video) value;
		JLabel both = new JLabel();
		dimension = both.getPreferredSize();
		dimension.height = imageSize.height + 1;
		both.setPreferredSize(dimension);
		both.setVerticalAlignment(JLabel.CENTER);
		both.setLayout(new BorderLayout());
		both.setOpaque(true);
		both.setBorder(matteBorder);
		final JLabel label = new JLabel(song.toString());
		label.setBorder(emptyBorder);
		final JLabel icon = new JLabel();
		icon.setPreferredSize(imageSize);
		icon.setMinimumSize(imageSize);
		icon.setMaximumSize(imageSize);
		icon.setHorizontalAlignment(JLabel.CENTER);
		if (song.icon != null){
			icon.setIcon(song.icon);
		}else{
			icon.setIcon(DEFAULT_DETAIL_ALBUM_ART);
			process.add(new Thread(){
				public void run(){
					icon.setIcon(song.getImage());
					list.repaint();
				}
			});
			synchronized (loadImages) {
				loadImages.notifyAll();
			}
		}
		both.add(icon, BorderLayout.WEST);
		both.add(label, BorderLayout.CENTER);
		if (isSelected) {
			both.setBackground(selected);
			label.setForeground(Color.WHITE);
		}else{
			both.setBackground(Color.WHITE);
		}
		return both;
	}
}
